package com.tip.dal.dao.ibatis;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.tip.dal.dao.ITipDAO;
import com.tip.dal.dataobject.TipDO;

public class IBatisTipDAO extends SqlMapClientDaoSupport implements ITipDAO {

	public void insertTipDO(final List<TipDO> tipDOList) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			public Object doInSqlMapClient(SqlMapExecutor executor)	throws SQLException {
				executor.startBatch();
				String str = "TIP.insert";
				for (TipDO tObject : tipDOList) {
					executor.insert(str, tObject);
				}
				return executor.executeBatch();
			}
		});
	}
	
	public Integer insertTipDO(TipDO tipDO) {
		Object newKey = getSqlMapClientTemplate().insert("TIP.insert",tipDO);
		return (Integer) newKey;
	}
	
	@SuppressWarnings("unchecked")
	public List<TipDO> listTipDO(TipDO tipDO) {
		return getSqlMapClientTemplate().queryForList("TIP.list", tipDO);
	}
	
    public TipDO findTipDOById(Integer id) {
        Object obj = getSqlMapClientTemplate().queryForObject("TIP.findById",id);
        return (TipDO)obj;
    }
    
    public int deleteTipDOById(Integer id) {
    	return getSqlMapClientTemplate().delete("TIP.deleteById", id);
    }
    
    public Integer updateTipDO(TipDO tipDO)
    {
        return getSqlMapClientTemplate().update("TIP.update", tipDO);
    }
}
